package com.kgc.controller;

import com.kgc.bean.User;
import com.kgc.config.annotation.RequestUser;
import com.kgc.service.FootPrintService;
import com.kgc.util.ReturnResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Classname FootPrintController
 * @Description TODO
 * @Date 2021/6/23 8:17
 * @Created by 17402
 */
@RestController
@Api(tags = "足迹功能")
@Slf4j
public class FootPrintController {
    @Autowired
    private FootPrintService footPrintService;

    //浏览商品
    @PostMapping("/browserProducts")
    @ApiOperation(value = "浏览商品",notes = "")
    @ApiResponses({
            @ApiResponse(code = 0000,message = "成功"),
            @ApiResponse(code = 9002,message = "token错误，重新登录"),
            @ApiResponse(code = 9003,message = "该商品不存在")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", required = true)
    })
    public ReturnResult<Map<String,Object>> browseProducts(String goodsId, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String token=req.getHeader("token");
        if(null==token){
            //未登录的话存在cookie里
            return footPrintService.addFootPrint2Cookie(goodsId,req,resp);

        }else {
            //登录的话存在数据库里
            return footPrintService.addFootPrint2Database(token,goodsId);

        }
    }

    //浏览商品
    @PostMapping("/browseFootprint")
    @ApiOperation(value = "浏览我的足迹",notes = "")
    @ApiResponses({
            @ApiResponse(code = 0000,message = "成功")
    })
    public ReturnResult<Map<String,Object>> browseFootprint(HttpServletRequest req, @RequestUser @ApiIgnore User user) throws Exception {
        if(null==user){
            //从cookie里读取
            return footPrintService.readFootPrintFromCookie(req);
        }else {
            //从数据库里读取
            return footPrintService.readFootPrintFromDatabase(user);

        }
    }


}
